package com.meltingslime.asciifight.Network;

/**
 * Author: LucaFraga
 * Date: 01/01/13 12.05
 */

import com.meltingslime.asciifight.Logger;
import com.meltingslime.asciifight.Network.Packets.Out.DisconnectPacket;
import com.meltingslime.asciifight.Network.Packets.Out.IPacketOut;
import com.meltingslime.asciifight.Network.Packets.Out.ServerConnectPacket;
import com.meltingslime.asciifight.client.ASCIIFight;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;


public class Client {
    private Thread _listeningThread;
    private Socket _client;
    private DataInputStream _inStream;
    private DataOutputStream _outStream;
    private String _userName;
    private boolean _connected;
    private Server _server;
    private Logger log;
    public String GetUserName()
    {
        return _userName;
    }
    public boolean IsConnected()
    {
        return _connected;
    }
    public Server GetServer()
    {
        return _server;
    }

    public Client(String userName) {
        _userName = userName;
        log = ASCIIFight.getLoggerInstance();
    }

    public void Connect(Server server) {
        _server = server;
        try {
            _client = new Socket(_server.IP, _server.Port);
        } catch (UnknownHostException uhE) {
            //log.WriteLine("Server at %s:%d cannot be found. Please check your internet connection.".format(_server.IP,_server.Port), Logger.LogTypes.Fatal);
            // TODO Wait for Localization
        } catch (IOException ioE) {
            // log.WriteLine("Cannot connect to %s:%d. Reason:%s".format(_server.IP, _server.Port, ioE.getMessage()), Logger.LogTypes.Fatal);
            // TODO Wait for localization
        }
        SendPacket(new ServerConnectPacket(_userName));
        _listeningThread = new Thread(new Listener(), "Listener Thread");
        _listeningThread.start();
    }

    public void Disconnect() {
        if (_connected)
            SendPacket(new DisconnectPacket());
        // TODO Edit when DisconnectPacket has a complete .ctor
    }

    public void Drop(String reason) {
        try {
            _inStream.close();
            _outStream.close();
            _client.close();

        } catch (IOException ignored) {
        } finally {
            log.WriteLine(String.format("Connection to server dropped. Reason:%s", reason));
            _connected = false;
            ASCIIFight.SetGamePhase(ASCIIFight.GamePhase.LobbyPhase);
        }
    }

    public void SendPacket(IPacketOut packet) //Packet packet)
    {
        try {
            _outStream.write(packet.getData());
        } catch (IOException e) {
            log.WriteLine(String.format("Cannot send packet to server. Reason:%s",e.getMessage()), Logger.LogTypes.Severe);
            Drop("Packet send failure.");
        }
    }
    protected class Listener implements Runnable {
        public void ReceiveData() {
            while (_connected) {
                 // TODO Read data and parse
            }
        }
        @Override
        public void run() {
            ReceiveData();
        }
    }



}
